﻿//namespace ContosoUniversity.DAL
//{
//    using System.Linq;
//    using ContosoUniversity.Models;
//    using ContosoUniversity.Services.QueryServices;
//    using ContosoUniversity.ViewModels;
//    using NRepository.Core.Query;
//    using NRepository.EntityFramework;

//    // Used to extend the DbContext while keeping a uniform repository experience
//    // This can be used to collate data from any source. Wg. Wcf, WebApi, MsMq, cloud, S3 blobs, etc.
//    public class ContosoUniversityQueryInterceptor : IQueryInterceptor
//    {
//        public IQueryable<T> Query<T>(IQueryRepository repository, IQueryable<T> query, object additionalQueryData) where T : class
//        {
//            if (typeof(T) == typeof(EnrollmentDateGroup))
//                return GetEnrollmentDateGroups<T>(repository);

//            //if (typeof(T) == typeof(CourseQueryModel))
//            //    return CourseQueryModels<T>(repository);

//            return query;
//        }

//        //private IQueryable<T> CourseQueryModels<T>(IQueryRepository repository) where T : class
//        //{
//        //    return (IQueryable<T>)repository.GetEntities<Course>()
//        //       .Select(course => new CourseQueryModel
//        //       {
//        //           CourseID = course.CourseID,
//        //           Credits = course.Credits,
//        //           DepartmentName = course.Department.Name,
//        //           Title = course.Title,
//        //           DepartmentID = course.DepartmentID,
//        //       });
//        //}

//        private static IQueryable<T> GetEnrollmentDateGroups<T>(IQueryRepository repository) where T : class
//        {
//            // Return readonly data
//            string sql = "SELECT EnrollmentDate, COUNT(*) AS StudentCount "
//               + "FROM Person "
//               + "WHERE Discriminator = 'Student' "
//               + "GROUP BY EnrollmentDate";

//            var enrollmentDateGroups = repository.ExecuteSqlQuery<EnrollmentDateGroup>(sql);
//            return (IQueryable<T>)enrollmentDateGroups.AsQueryable();
//            
//            // Can also use  as below
//            //var enrollmentDateGroupsUsingLinq = from student in repository.GetEntities<Student>()
//            //                                    group student by student.EnrollmentDate into dateGroup
//            //                                    select new EnrollmentDateGroup()
//            //                                    {
//            //                                        EnrollmentDate = dateGroup.Key,
//            //                                        StudentCount = dateGroup.Count()
//            //                                    };
//        }
//    }
//}